package my.peerencrypt;


public class Encrypt {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String src = "3214-8536-4621-7235";
		System.out.println(src);
		//System.out.println(Arrays.toString(iarr));
		int b[] = new int[11];
		
		int[] iarr = strToIntArr(src);
		src = encrypt(iarr,b);
		
		
		iarr = new int[16];
		
		for(int i=0,j=0;i<19;i++){
			if((i+1)%5==0)continue;
			iarr[j++] = Integer.valueOf(String.valueOf(src.charAt(i)));
		}
		decrypt(iarr,b);
	}

	
	public static int[] strToIntArr(String src){
		
		int[] iarr = new int[16];
		
		for(int i=0,j=0;i<19;i++){
			if((i+1)%5==0)continue;
			iarr[j++] = Integer.valueOf(String.valueOf(src.charAt(i)));
		}
		return iarr;
	}
	
	
	public static String decrypt(int[] ec,int[] key){
		StringBuilder sb = new StringBuilder();
		
		int i=0;
		int k =0;
		int[] a = new int[4];
		//(a*2-1)%4,b%5+1,(c*11+3)%7,(d+1)*5%3
		a[0] = (key[k++]*4+ec[i++]+1)/2;
		a[1] = key[k++]*5 + ec[i++]-1;
		a[2] = (key[k++]*7+ec[i++]-3)/11;
		a[3] = (key[k++]*3+ec[i++])/5-1;
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		sb.append("-");
		
		
		//(a*6+2)%2,(b+5)%8,c*7%3,d
		a[0] = (key[k++]*2+ec[i++]-2)/6;
		a[1] = key[k++]*8+ec[i++]-5;
		a[2] = (key[k++]*3 + ec[i++])/7;
		a[3] = ec[i++];
		
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		//sb2.append(b[0]).append(b[1]).append(b[2]).append("-");
		sb.append("-");
		
		//第三个4位4621加密算法为“第一位与第四位调换，第二位与第三位调换
		a[3] = ec[i++];
		a[2] = ec[i++];
		a[1] = ec[i++];
		a[0] = ec[i++];
		
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		sb.append("-");
		
		//a*13%9,(b+100)%8,（c*23+2)%6，（d+17）%5
		a[0] = (key[k++]*9+ec[i++])/13;
		a[1] = (key[k++]*8 +ec[i++])-100;
		a[2] = (key[k++]*6+ec[i++]-2)/23;
		a[3] = key[k++]*5+ec[i++]-17;
		
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		//1301-0206-1264-1652
		return sb.toString();
	}
	
	
	public static String encrypt(int[] src,int[] key){
		StringBuilder sb = new StringBuilder();
		
		int[] a = new int[4];
		int i=0;
		int k =0;
		
		//(a*2-1)%4,b%5+1,(c*11+3)%7,(d+1)*5%3
		a[0] = (src[i++]*2-1)%4;
		a[1] = src[i++]%5+1;
		a[2] = (src[i++]*11+3)%7;
		a[3] = (src[i++]+1)*5%3;
		i-=4;
		key[k++] = (src[i++]*2-1)/4;
		key[k++] = src[i++]/5;
		key[k++] = (src[i++]*11+3)/7;
		key[k++] = (src[i++]+1)*5/3;
		//sb2.append(b[0]).append(b[1]).append(b[2]).append(b[3]).append("-");
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		sb.append("-");
		
		
		//(a*6+2)%2,(b+5)%8,c*7%3,d
		a[0] = (src[i++]*6+2)%2;
		a[1] = (src[i++]+5)%8;
		a[2] = src[i++]*7%3;
		a[3] = src[i++];
		
		i-=4;
		key[k++] = (src[i++]*6+2)/2;
		key[k++] = (src[i++]+5)/8;
		key[k++] = src[i++]*7/3;
		i++;
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		//sb2.append(b[0]).append(b[1]).append(b[2]).append("-");
		sb.append("-");
		
		//第三个4位4621加密算法为“第一位与第四位调换，第二位与第三位调换
		a[3] = src[i++];
		a[2] = src[i++];
		a[1] = src[i++];
		a[0] = src[i++];
				
		
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		sb.append("-");
		
		//a*13%9,(b+100)%8,（c*23+2)%6，（d+17）%5
		a[0] = src[i++]*13%9;
		a[1] = (src[i++]+100)%8;
		a[2] = (src[i++]*23+2)%6;
		a[3] = (src[i++]+17)%5;
		
		i-=4;
		key[k++] = src[i++]*13/9;
		key[k++] = (src[i++]+100)/8;
		key[k++] = (src[i++]*23+2)/6;
		key[k++] = (src[i++]+17)/5;
		//sb2.append(b[0]).append(b[1]).append(b[2]).append(b[3]);
		for(int j=0;j<4;j++){
			sb.append(a[j]);
		}
		//1301-0206-1264-1652
		return sb.toString();
	}
}
